package dth.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.Faq;
import dth.entity.Package;

public class FaqDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	public java.util.List<Faq> getListFaq()
	{
		session.beginTransaction();
		String queryString="select * from faq";
		Query query=session.createSQLQuery(queryString).addEntity(Faq.class);
		return query.list();
	}
	public int insert(Faq faq)
	{
		Transaction tx=session.beginTransaction();
		String queryString="INSERT into faq VALUES(0,1,:content,:date_insert,:title)";
		System.out.println("-----INSERT into faq VALUES(0,1,"+faq.getFaqContent()+","+faq.getDateInsert()+","+faq.getFaqTitle());
		Query query=session.createSQLQuery(queryString);
		query.setParameter("content",faq.getFaqContent());
		query.setParameter("date_insert",faq.getDateInsert());
		query.setParameter("title",faq.getFaqTitle());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int update(Faq faq)
	{
		Transaction tx=session.beginTransaction();
		String queryString="update faq set faq_title=:title,faq_content=:content,date_insert=:date_insert where faq_id=:id";
		Query query=session.createSQLQuery(queryString);
		query.setParameter("id",faq.getFaqId());
		query.setParameter("content",faq.getFaqContent());
		query.setParameter("date_insert",faq.getDateInsert());
		query.setParameter("title",faq.getFaqTitle());
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int delete(int id)
	{
		Transaction tx=session.beginTransaction();
		String queryString="delete from faq where faq_id= :id";
		Query query=session.createSQLQuery(queryString).setParameter("id", id);
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
}
